<?php
class Zendvn_Model_Table_Tags extends Zend_Db_Table{
	protected $_name = "tags";
	protected $_primary = "id"; 
	protected $_arrParam = array();
	
	public $_searchFields = array('name'=>'- Name field','code'=>'- Code field');
	
	protected $_listSQL;
	
	public function deleteItem($arrParam = null,$options = null){
		$model = new Zendvn_Model_DeleteRecode();
		$model->newsItem($arrParam,$options,$this);
	}
	public function getItem($arrParam = null,$options = null){
		if($options == null){
			$where = 'id = ' . (int)$arrParam['id'];
			$result = $this->fetchRow($where);
			if(!empty($result))
		        $result = $result->toArray();
		}		
		if($options['task'] == 'alias'){
			$db = $this->getAdapter();
			$select = $db->select()
			            ->from(array('i' => $this->_name))
			            ->where('i.alias = ?', $arrParam['alias'], STRING);
			
			$result = $db->fetchRow($select);
		}
		return $result;
	}
	
	public function listItem($arrParam = null,$options = null){
		if($options == null){
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from(array('i' => $this->_name));
			$arrFilter = $arrParam['arrFilter'];
			
			if(!empty($arrFilter['col'])){
				$select->order($arrFilter['col'] . ' ' . $arrFilter['order']);			
			}
	
			if(!empty($arrFilter['status']) && $arrFilter['status'] != 'none'){
				$select->where('i.status = ?',$arrFilter['status']);
			}
			
			if(!empty($arrFilter['keyword']) && !empty($arrFilter['searchField'])){
				$keyword = '%' . $arrFilter['keyword'] . '%';
				$select->where($arrFilter['searchField'] . ' LIKE ? ', $keyword);
			}
			
			$this->_listSQL = $select;
			$panigator = $arrParam['panigator'];	
			$select->limitPage($panigator['page'], $panigator['rowCount']);
			
			$result = $db->fetchAll($select);		 
		}
		
		if($options['task'] == 'get-tags-json'){
			$db = $this->getAdapter();
			$select = $db->select()
							->from(array('i' => $this->_name), array('id', 'name AS label', 'name AS value'));
			$result = $db->fetchAll($select);
		}
		
		if($options['task'] == 'popular-tags'){
			$db = $this->getAdapter();
			$select = $db->select()
			        ->from(array('i' => $this->_name), array('id', 'name', 'alias'))
			        ->order('i.view DESC')
			        ->limit(10, 0);
			$result = $db->fetchAll($select);
		}
		
		return $result;
	}
	
	public function totalItem($arrParam = null,$options = null){
		
		if($options == null){
			$db = $this->getAdapter();
			$select = $this->_listSQL->reset(Zend_Db_Select::COLUMNS);
			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_COUNT);
			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_OFFSET);
			$select = $this->_listSQL->reset(Zend_Db_Select::ORDER);
			$select = $this->_listSQL->reset(Zend_Db_Select::GROUP);
			$select->columns('COUNT(i.id) AS total');	
			$result = $db->fetchOne($select);
		}
		return $result;
		
	}
	
	public function saveItem($arrParam = null,$options = null){
		
		$function  = Zendvn_System_Function::call($options['task']);
		return $this->$function($arrParam);
	}
	
	public function adminAdd($arrParam = null){
		$info = new Zendvn_System_Info();
		$idUser = $info->getMemberInfo('id');
		
		$row = $this->fetchNew();
		$row->name			= $arrParam['name'];			
		$row->alias			= $arrParam['alias'];
		$row->view			= $arrParam['view'];
		$row->html_descript	= $arrParam['html_descript'];
		$row->html_keyword	= $arrParam['html_keyword'];
		return $row->save();
	}
	
	public function adminEdit($arrParam = null){
		$info 				= new Zendvn_System_Info();
		$idUser 			= $info->getMemberInfo('id');
		
		$where  			= 'id = ' . (int)$arrParam['id'];
		$row 				= $this->fetchRow($where);
		$row->name			= $arrParam['name'];	
		$row->alias			= $arrParam['alias'];		
		$row->view			= $arrParam['view'];
		$row->html_descript	= $arrParam['html_descript'];
		$row->html_keyword	= $arrParam['html_keyword'];
		return $row->save();
	}
}